package com.atguigu.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.io.IOException;

public class EsTestQuery {

    /**
     * 高级查询  全量查询
     * @throws IOException
     */
    @Test
    public void queryAll() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        // 查询索引中全部数据
        SearchRequest request = new SearchRequest();
        request.indices("user");// 指定索引

        // 构造查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());
        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数："+hits.getTotalHits());
        System.out.println("所需时间："+response.getTook());

        System.out.println("===============>");
        for (SearchHit searchHits : hits) {
            System.out.println(searchHits);
        }
    }

    /**
     * 高级查询  条件查询
     * @throws IOException
     */
    @Test
    public void query() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        // 条件查询
        SearchRequest request = new SearchRequest();
        request.indices("user");// 指定索引

        // 构造查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.termQuery("age", 44));
        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数："+hits.getTotalHits());
        System.out.println("所需时间："+response.getTook());

        System.out.println("===============>");
        for (SearchHit searchHits : hits) {
            System.out.println(searchHits);
        }
    }

    /**
     * 高级查询  分页查询 排序
     * @throws IOException
     */
    @Test
    public void queryPage() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        // 条件查询
        SearchRequest request = new SearchRequest();
        request.indices("user");// 指定索引

        // 构造查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        // （当前页码-1）*每页显示数据条数
        builder.from(0);
        builder.size(10);
        // 排序
        builder.sort("age",SortOrder.DESC);
        // 条件查询
        String[] include = {};
        String[] exclude = {"age"};
        builder.fetchSource(include, exclude);
        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数："+hits.getTotalHits());
        System.out.println("所需时间："+response.getTook());

        System.out.println("===============>");
        for (SearchHit searchHits : hits) {
            System.out.println(searchHits);
        }
    }

    /**
     * 高级查询 组合查询 范围查询
     * @throws IOException
     */
    @Test
    public void queryAnd() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        // 条件查询
        SearchRequest request = new SearchRequest();
        request.indices("user");// 指定索引

        // 组合查询
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //boolQueryBuilder.mustNot(QueryBuilders.matchQuery("age", 33));
        //boolQueryBuilder.must(QueryBuilders.matchQuery("gender", "male"));

        // 或
        //boolQueryBuilder.should(QueryBuilders.matchQuery("age", 33));
        //boolQueryBuilder.should(QueryBuilders.matchQuery("age", 44));
        //builder.query(boolQueryBuilder);

        // 范围查询
        //RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");

        //rangeQuery.gte(40);// >=
        //rangeQuery.lte(60);// <=

        //builder.query(rangeQuery);

        // 模糊查询
        //FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "朱元").fuzziness(Fuzziness.ONE);

        //builder.query(fuzziness);

        // 高亮查询
        TermsQueryBuilder termsQuery = QueryBuilders.termsQuery("name", "元");

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name");
        builder.highlighter(highlightBuilder);
        builder.query(termsQuery);

        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数："+hits.getTotalHits());
        System.out.println("所需时间："+response.getTook());

        System.out.println("===============>");
        for (SearchHit searchHits : hits) {
            System.out.println(searchHits);
        }
    }
}
